<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>表情</title>
    <style>
        * {
            padding: 0;
            margin: 0;
        }

        .list {
            display: flex;
            text-align: center;
        }
    </style>
</head>

<body>
    <div class="list"></div>
    <button class="prev">上一页</button>
    <button class="next">下一页</button>
    <span class="num"></span>
</body>

</html>
<script src="./ajax-promise.js"></script>
<script>

    let page = 1;
    const list = document.querySelector('.list');
    const prev = document.querySelector('.prev');
    const next = document.querySelector('.next');
    const num = document.querySelector('.num');
    //封装请求函数
    function getajax(pageStatus) {
        ajax('get', 'https://yantianfeng.com/api/emotion', { page: pageStatus, count: 10 }).then((res) => {
            let str = res.emotions.map((item, index) => {
                return `<div><img src='${item.url}'/><div>${item.title}</div></div>`
            }).join('');
            list.innerHTML = str;
        })
    }

    //初始状态
    getajax(page);
    prev.disabled = true;
    num.innerHTML = `${page}/4`;

    //点击事件
    next.onclick = () => {
        page++;
        getajax(page);
        num.innerHTML = `${page}/4`;
        if (page > 1){prev.disabled = false;}
        if(page == 4){next.disabled = true;}
    }
    prev.onclick = () => {
        page--;
        getajax(page);
        num.innerHTML = `${page}/4`;
        if (page == 1){prev.disabled = true;next.disabled = false;}
    }

</script>